package org.jeecg.modules.xxgx.djgl.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xkcoding.http.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.common.util.LoginUserUtil;
import org.jeecg.modules.common.util.StringUtilsExt;
import org.jeecg.modules.qyxx.entity.CsPtQyxx;
import org.jeecg.modules.xxgx.common.util.*;
import org.jeecg.modules.xxgx.djgl.constant.StringConstant;
import org.jeecg.modules.xxgx.djgl.dto.uploadFileDto;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdj;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjExt;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjmx;
import org.jeecg.modules.xxgx.djgl.entity.CsXxYkdj;
import org.jeecg.modules.xxgx.djgl.entity.tdyw.*;
import org.jeecg.modules.xxgx.djgl.enums.PzlxEnum;
import org.jeecg.modules.xxgx.djgl.enums.PzlyEnum;
import org.jeecg.modules.xxgx.djgl.enums.ZsfsEnum;
import org.jeecg.modules.xxgx.djgl.service.*;
import org.jeecg.modules.xxgx.djgl.vo.CepzmxVo;
import org.jeecg.modules.xxgx.djgl.vo.Ret;
import org.jeecg.modules.xxgx.khgl.entity.CsXxCustom;
import org.jeecg.modules.xxgx.khgl.service.ICsXxCustomService;
import org.jeecg.modules.xxgx.spgl.entity.CsXxProduct;
import org.jeecg.modules.xxgx.spgl.service.ICsXxProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class UploadFileImpl implements IUploadFile {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private ICsXxDkdjService iCsXxDkdjService;

    @Autowired
    private ICsXxProductService iCsXxProductService;

    @Autowired
    private ICsXxCustomService iCsXxCustomService;

    @Autowired
    private ICsXxDkdjmxService iCsXxDkdjmxService;

    @Autowired
    private ICsXxDkdjExtService iCsXxDkdjExtService;

    @Autowired
    private ICsXxYkdjService iCsXxYkdjService;

    @Override
    public File uploadFile(MultipartFile file, uploadFileDto dto) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String excelDrMark = "2";
        //1为含税导入，0为不含税导入
        String judgeHanshui = dto.getPrice();
        //发票类型代码
        String fplxdm = dto.getFplxdm();
        //导入日期
        String dataStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //特殊票种 02收购票 08成品油 12机动车
        String tspz = dto.getTspz();
        //数据类型
        String sjlx = Convert.toStr(dto.getSjlx(), "0");
        //是否数电纸票:0不是 1是
        String sfwzp = dto.getSfwzp();
        Map<String, List<Tdywlx>> tdywMap = readSpecialInfoJson(file, fplxdm);
        Map<String, List<CepzmxVo>> cepzmxVoList = readCepzmx(file);
        //开票方式
        String kpfs = "1";
        //返回的报错信息
        String msg = "";
        ArrayList<Object> list = new ArrayList<Object>();
        File FileValue = null;

        if (StringUtils.isEmpty(tspz)) {
            tspz = "00";
        }
        String tenant_id = user.getLoginTenantId().toString();

        try {
            FileValue = FileHelper.MutlFileToFile(file);//br-获取上传文件
        } catch (Exception e) {
            e.printStackTrace();
            throw new JeecgBootException(e);
        }
        String fileName = FileValue.toString();//br-获取上传文件的文件名
        String fileNameTail = fileName.substring(fileName.lastIndexOf(".") + 1);//获取上传文件的文件名称后缀，根据后缀进行判断执行不通的操作
        if ("xls".equalsIgnoreCase(fileNameTail) || "xlsx".equalsIgnoreCase(fileNameTail)) {
            try {
                logger.info("{}excel格式的文件处理", "------------------------------------");
                list = (ArrayList<Object>) getAllExcelRowFpsqdgl(FileValue, 0, 2);
                logger.info("{}excel格式的文件处理完成！共计：{}行~", "-----------------------------", list == null ? "0" : list.size());
            } catch (IOException e) {
                e.printStackTrace();
                throw new JeecgBootException(e);
            }
        } else {
            throw new JeecgBootException("暂不支持除xls和xlsx以外的格式！");
        }

        //数据错误行数
        int errorRowIndex = 0;
        //数据错误的列数
        int errorColIndex = 0;
        //数据错误的列的标题
        String errorColTitle = "";
        Set<String> cezsDjs = new HashSet<>();
        String djbh;
        String khmc;
        String kce;

        //默认普通征税
        String zsfs = ZsfsEnum.PTZS.getValue();

        int checkOk = 1;

        for (int i = 0; i < list.size(); i++) {
            errorRowIndex = i;
            ArrayList<Object> rowList = (ArrayList) list.get(i);
            djbh = StringUtilsExt.getString(rowList.get(0));
            khmc = StringUtilsExt.getString(rowList.get(2));

            //判断客户名称、商品名称、数量、金额、税率 是否为空
            if (org.springframework.util.StringUtils.isEmpty(rowList.get(2)) || org.springframework.util.StringUtils.isEmpty(rowList.get(7)) || org.springframework.util.StringUtils.isEmpty(rowList.get(12)) || org.springframework.util.StringUtils.isEmpty(rowList.get(13))) {
                checkOk = 0;
                break;
            }

            //检验是否是差额征税
            kce = StringUtilsExt.getString(rowList.get(25));
            if (rowList.size() > 36 && !org.springframework.util.StringUtils.isEmpty(rowList.get(36))) {
                zsfs = StringUtilsExt.getString(rowList.get(36));
            }

            //-----------------------------------根据获取excel获取到的数据  进行校验  开始---------------------------------------------------

            //检验差额征税 是否是含税导入
            if (!StringUtils.isEmpty(kce)) {
                if (!"1".equals(judgeHanshui)) {
                    checkOk = -10;
                    msg = "当征税方式为“差额征税”时，请选择含税导入";
                    break;
                }
                //差额征税不能开具折扣票
                if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || !org.springframework.util.StringUtils.isEmpty(rowList.get(24)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                    checkOk = -11;
                    break;
                }
                //差额征税只能为1行明细
                if (org.springframework.util.StringUtils.isEmpty(djbh)) {
                    if (cezsDjs.contains(khmc)) {
                        checkOk = -12;
                        msg = "当征税方式为“差额征税”时，客户名称：" + khmc + "，不能存在多行";
                        break;
                    }
                    cezsDjs.add(khmc);
                } else {
                    if (cezsDjs.contains(djbh)) {
                        checkOk = -12;
                        msg = "当征税方式为“差额征税”时，单据编号：" + djbh + "，不能存在多行";
                        break;
                    }
                    cezsDjs.add(djbh);
                }
                //判断是否为全单开具差额征收，（全电开具差额征收，征收方式不能为空，并且差额明细不能为空）
                if (StrUtil.equals("01", fplxdm) || StrUtil.equals("02", fplxdm)) {
                    ZsfsEnum zsfsEnum = ZsfsEnum.getDataByName(zsfs);
                    if (ObjectUtil.isNotNull(zsfsEnum)) {
                        zsfs = zsfsEnum.getValue();
                        if (!(StrUtil.equals(ZsfsEnum.CEZS.getValue(), zsfs)
                                || StrUtil.equals(ZsfsEnum.QEZS.getValue(), zsfs))) {
                            checkOk = -12;
                            msg = "当征税方式为“差额征税并且开具全电票”时，单据编号：" + djbh + "征收方式不合法";
                            break;
                        }
                    } else {
                        checkOk = -12;
                        msg = "当征税方式为“差额征税并且开具全电票”时，单据编号：" + djbh + "征收方式不合法";
                        break;
                    }
                    if (CollUtil.isEmpty(cepzmxVoList.get(djbh))) {
                        checkOk = -12;
                        msg = "当征税方式为“差额征税并且开具全电票”时，单据编号：" + djbh + "征收明细不能为空";
                        break;
                    }
                    try {
                        validateCepzmx(cepzmxVoList.get(djbh));
                    } catch (JeecgBootException e) {
                        checkOk = -12;
                        msg = "当征税方式为“差额征税并且开具全电票”时，单据编号：" + e.getMessage();
                        break;
                    }
                }
            }

            /**
             * 判断数量、单价、金额、税率列的值是否为数字格式
             */
            try {
                Double.valueOf(rowList.get(12) + "");
                Double.valueOf(rowList.get(13) + "");
                if ((rowList.get(13) + "").indexOf(".") < 1 && !(rowList.get(13) + "").equals("0")) {//br-多加了个判断条件
                    checkOk = -4;
                    break;
                }
                //扣除额 是否为数字
                if (!StringUtils.isEmpty(kce)) {
                    Double.valueOf(kce);
                }
            } catch (NumberFormatException nfe) {
                logger.error(nfe.getMessage());
                checkOk = -4;
                break;
            }

            /**
             * 判断数字格式是否正确
             */
            try {
                errorColTitle = "使用优惠政策";
                Double.valueOf(org.springframework.util.StringUtils.isEmpty(rowList.get(15)) ? "0" : rowList.get(15) + "");
            } catch (NumberFormatException nfe) {
                logger.error(nfe.getMessage());
                checkOk = -1;
                break;
            }

            //数量*单价 = 金额的校验
            if (!(rowList.get(10) + "").equals("") && !(rowList.get(11) + "").equals("")) {//br-添加了一层非空判断
//                    直接用double类型做乘法运算会出现精度丢失
                BigDecimal sl = new BigDecimal((String) rowList.get(10));
                BigDecimal dj = new BigDecimal((String) rowList.get(11));
                double cj = Double.parseDouble(sl.multiply(dj).toString());
                double wc = CommonUtil.subZeroAndDot(Double.valueOf((String) rowList.get(12))) - cj;
                //误差大于一分钱
                if (wc > 0.01) {
                    checkOk = -6;
                    break;
                }
            }

            //校验特殊票种“成品油”的时候计量单位只能为吨或升-rowList.get(9)
            if (tspz.equals("08") && !(rowList.get(9) + "").equals("吨") && !(rowList.get(9) + "").equals("升")) {
                checkOk = -7;
                break;
            }

            int djbhCount = iCsXxDkdjService.lambdaQuery().eq(CsXxDkdj::getYwdjbh, djbh).list().size();

            int ykdjbhCount = iCsXxYkdjService.lambdaQuery().eq(CsXxYkdj::getYwdjbh, djbh).list().size();

            if (!org.springframework.util.StringUtils.isEmpty(djbh) && (djbhCount > 0 || ykdjbhCount > 0)) {
                checkOk = -9;
                msg = "单据编号：" + djbh + "，已存在";
                break;
            }

            List<Tdywlx> tdywlxList = tdywMap.get(djbh);
            if (tdywlxList != null && !tdywlxList.isEmpty()) {
                Map<? extends Class<? extends Tdywlx>, List<Tdywlx>> clazz = tdywlxList.stream().collect(Collectors.groupingBy(Tdywlx::getClass));
                if (clazz.size() > 1) {
                    checkOk = -9;
                    msg = "单据编号：" + djbh + "，不能同时存在多个不同的特定业务明细";
                    break;
                }
                Tdywlx tdyw = tdywlxList.get(0);
                if (org.apache.commons.lang3.StringUtils.equals(tdyw.getDataType(), "object")) {
                    if (tdywlxList.size() > 1) {
                        checkOk = -9;
                        msg = "单据编号：" + djbh + "，不能同时存在多个特定业务明细";
                        break;
                    }
                }
            }
            //-----------------------------------根据获取excel获取到的数据  进行校验  结束---------------------------------------------------
        }

        if (checkOk == 0) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {//br-根据不同的导入格式进行不同的赋值
                msg = "第" + (errorRowIndex + 2) + "行，缺少必填项 或为空";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "xml文件中某项的值不能为空，请仔细检查";
            } else {
                msg = "json文件中某项的值不能为空，请仔细检查";
            }
        } else if (checkOk == -1) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "第 " + (errorRowIndex + 2) + " 行，《" + errorColTitle + "》列，不符合数字格式";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "xml文件中某项值的格式不对，请仔细检查";
            } else {
                msg = "json文件中某项值的格式不对，请仔细检查";
            }
        } else if (checkOk == -2) {
            msg = "你上传的文件格式不对，请重新选择上传";
        } else if (checkOk == -3) {
            msg = "未获取到登录账户信息，请重新登录后上传文件";
        } else if (checkOk == -4) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "第 " + (errorRowIndex + 2) + " 行，数量、单价、金额、税率、扣除额某一列或几列，不符合数字格式";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "xml文件中数量、单价、金额、税率、扣除额某一项或几项，不符合数字格式，请仔细检查";
            } else {
                msg = "json文件中数量、单价、金额、税率、扣除额某一项或几项，不符合数字格式，请仔细检查";
            }
        } else if (checkOk == -5) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "第 " + (errorRowIndex + 2) + " 行，当数量列不为空时，单价列不能为空，请仔细检查";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "xml文件中当数量项不为空时，单价项不能为空，请仔细检查";
            } else {
                msg = "json文件中当数量项不为空时，单价项不能为空，请仔细检查";
            }
        } else if (checkOk == -6) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "第 " + (errorRowIndex + 2) + " 行，数量*单价不等于金额，请仔细检查";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "xml文件中存在数量*单价不等于金额，请仔细检查";
            } else {
                msg = "json文件中存在数量*单价不等于金额，请仔细检查";
            }
        } else if (checkOk == -7) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "当特殊票种为“成品油时”，第 " + (errorRowIndex + 2) + " 行第10列，计量单位只能为“吨”或“升”，不可为空或其他单位，请仔细检查";
            } else if (fileNameTail.equalsIgnoreCase("xml")) {
                msg = "当特殊票种为“成品油时”，xml文件中计量单位只能为“吨”或“升”，不可为空或其他单位，请仔细检查";
            } else {
                msg = "当特殊票种为“成品油时”，json文件中计量单位只能为“吨”或“升”，不可为空或其他单位，请仔细检查";
            }
        } else if (checkOk == -11) {
            if (fileNameTail.equalsIgnoreCase("xls") || fileNameTail.equalsIgnoreCase("xlsx")) {
                msg = "当征税方式为“差额征税”时”，第 " + (errorRowIndex + 2) + " 行不能有折扣，请仔细检查";
            } else {
                msg = "当征税方式为“差额征税”时，不能有折扣，请仔细检查";
            }
        } else if (checkOk == -9 || checkOk == -10 || checkOk == -12) {

        } else {

            //业务校验没有问题  开始保存业务处理

            List<Map<String, Object>> fpsqdglList = new ArrayList<>();//br-建立一个list用于临时存储遍历数据
            List<Map<String, Object>> fpsqdglMxList = new ArrayList<>();//br-建立一个list用于临时存储遍历数据

            for (int i = 0; i < list.size(); i++) {
                ArrayList<Object> rowList = (ArrayList) list.get(i);

                //对使用优惠政策、优惠政策、免税类型进行判断
                String syyhzc = rowList.get(15).toString();
                String yhzc = rowList.get(16).toString();
                String mslx = rowList.get(17).toString();
                String je_str = rowList.get(12).toString();//br-从excel中获取金额
                String zkh_je_str = rowList.get(23).toString();//br-折扣行金额
                if (rowList.size() > 35) {
                    String zkl_str = rowList.get(35).toString();//br-折扣率
                    //当折扣金额为空 && 折扣率不为空时   计算折扣金额
                    if (StringUtil.isEmpty(zkh_je_str) && StringUtil.isNotEmpty(zkl_str) && StringUtil.isNotEmpty(je_str)) {
                        Double hsje_double_js = NumberUtils.toDouble(je_str);
                        Double zkl_double = NumberUtils.toDouble(zkl_str);
                        Double zkje_double = Double.parseDouble(String.format("%.2f", hsje_double_js * zkl_double));//计算折扣金额
                        zkh_je_str = String.format("%.2f", zkje_double);
                    }
                }

                if ("是".equals(syyhzc) || "1".equals(syyhzc)) {
                    syyhzc = "1";
                } else if ("否".equals(syyhzc) || "0".equals(syyhzc)) {
                    syyhzc = "0";
                }

                if ("免税".equals(mslx) || "1".equals(mslx)) {
                    mslx = "1";
                } else if ("不征税".equals(mslx) || "2".equals(mslx)) {
                    mslx = "2";
                } else if ("普通零税率".equals(mslx) || "3".equals(mslx)) {
                    mslx = "3";
                } else if ("".equals(mslx) || "正常税率".equals(mslx)) {
                    mslx = "";
                }

                //br-从excel中获取到相应的金额、税率、商品数量
                String tax_str = rowList.get(13).toString();//br-从excel中获取税率

                String spsl_str = rowList.get(10).toString();//br-从excle中获取商品数量
                String dj_str = rowList.get(11).toString();//br-从excle中获取单价

                Double hsje_double = null;//br-含税金额
                Double bhsje_double = null;//br-不含税金额
                String hsdj_str = "";
                String bhsdj_str = "";
                Double se_double = null;//br-税额

                Double zkh_hsje_double = null;
                Double zkh_bhsje_double = null;
                Double zkh_se_double = null;

                //br-这里需要判断是选择的含税导入还是不含税导入，来进行含税和不含税相关数据的计算

                if ("1".equals(judgeHanshui)) {
                    //含税
                    Map itemMap = new HashMap();
                    itemMap.put("tax", tax_str);
                    itemMap.put("spsl", spsl_str);
                    itemMap.put("hsje", je_str);
                    itemMap.put("bhsje", "");

                    itemMap.put("hsdj", dj_str);
                    itemMap.put("bhsdj", "");
                    itemMap.put("tspz", tspz);
                    //差额征税
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(25)) && !ZsfsEnum.QEZS.getValue().equals(zsfs)) {
                        FPUtil.calcMxForCezs(itemMap, StringUtilsExt.getString(rowList.get(25), "0"));
                    } else {
                        //自动计算工具类
                        FPUtil.calcItem(itemMap);
                    }
                    hsje_double = Double.parseDouble(StringUtilsExt.getString(itemMap.get("hsje")));//br-计算含税金额
                    bhsje_double = Double.parseDouble(StringUtilsExt.getString(itemMap.get("bhsje")));//br-计算不含税金额  将计算结果四舍五入保留两位小数 2020-5-24 11:01:03 rdd
                    hsdj_str = StringUtilsExt.getString(itemMap.get("hsdj"));
                    bhsdj_str = StringUtilsExt.getString(itemMap.get("bhsdj"));
                    spsl_str = StringUtilsExt.getString(itemMap.get("spsl"));
                    se_double = NumberUtils.toDouble(StringUtilsExt.getString(itemMap.get("se")));//br-计算税额，利用含税金额-不含税金额


                    //br-折扣行相关数据的计算
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                        zkh_hsje_double = -Double.parseDouble(zkh_je_str);//br-折扣行含税金额
                        zkh_bhsje_double = Double.parseDouble(String.format("%.2f", zkh_hsje_double / (1 + Double.parseDouble(tax_str))));//br-折扣行不含税金额
                        zkh_se_double = Double.parseDouble(String.format("%.2f", zkh_hsje_double - zkh_bhsje_double));//br-折扣行税额
                    }
                } else {
                    //不含税
                    Map itemMap = new HashMap();
                    itemMap.put("tax", tax_str);
                    itemMap.put("spsl", spsl_str);
                    itemMap.put("hsje", "");
                    itemMap.put("bhsje", je_str);

                    itemMap.put("hsdj", "");
                    itemMap.put("bhsdj", dj_str);

                    //自动计算工具类
                    itemMap.put("tspz", tspz);
                    FPUtil.calcItem(itemMap);
                    hsje_double = Double.parseDouble(StringUtilsExt.getString(itemMap.get("hsje")));//br-计算含税金额
                    bhsje_double = Double.parseDouble(StringUtilsExt.getString(itemMap.get("bhsje")));//br-计算不含税金额  将计算结果四舍五入保留两位小数 2020-5-24 11:01:03 rdd
                    hsdj_str = StringUtilsExt.getString(itemMap.get("hsdj"));
                    bhsdj_str = StringUtilsExt.getString(itemMap.get("bhsdj"));
                    spsl_str = StringUtilsExt.getString(itemMap.get("spsl"));
                    se_double = NumberUtils.toDouble(StringUtilsExt.getString(itemMap.get("se")));//br-计算税额，利用含税金额-不含税金额

                    //br-折扣行相关数据的计算
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                        zkh_bhsje_double = -Double.parseDouble(zkh_je_str);//br-折扣行不含税金额
                        zkh_hsje_double = Double.parseDouble(String.format("%.2f", zkh_bhsje_double * (1 + Double.parseDouble(tax_str))));//br-折扣行含税金额
                        zkh_se_double = Double.parseDouble(String.format("%.2f", zkh_hsje_double - zkh_bhsje_double));//br-折扣行税额
                    }
                }
                rowList.set(10, spsl_str);

                if (fpsqdglList.size() == 0) {
                    int djhhValue = 1;//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                    int djhhTotal = 0;

                    Map<String, Object> fpsqdglmap = new HashMap<>();
                    Map<String, Object> fpsqdglmxmap = new HashMap<>();
                    Map<String, Object> zkhMap = new HashMap<>();

                    String uuidValue = IdWorker.getIdStr();

                    Double hsje_sum_double = null;//br-含税总金额的计算，保存到主表用
                    Double bhsje_sum_double = null;//br-不含税总金额的计算，保存到主表用
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {//br-有折扣行的需要在极速验主表的含税接的时候添加上折扣行金额
                        hsje_sum_double = Double.parseDouble(String.format("%.2f", hsje_double + zkh_hsje_double));//br-计算保存到主表的“含税金额”的总和
                        bhsje_sum_double = Double.parseDouble(String.format("%.2f", bhsje_double + zkh_bhsje_double));//br-计算保存到主表的“不含税金额”的总和
                        djhhTotal = djhhValue + 1;
                    } else {//br-没有折扣行的时候正常计算
                        hsje_sum_double = hsje_double;//br-计算保存到主表的“含税金额”的总和
                        bhsje_sum_double = bhsje_double;//br-计算保存到主表的“不含税金额”的总和
                        djhhTotal = djhhValue;
                    }

                    djbh = (String) rowList.get(0);
                    String specialInfoJson = getSpecialInfoJson(djbh, tdywMap);
                    String tdywlx = getTdywlxDm(djbh, tdywMap);

                    fpsqdglmap.put("order", i);//br-按照新需求，用于记录添加的行数标记，用于判断无订单编号名称相同，是否是紧挨着的几个
                    fpsqdglmap.put("sqdid", uuidValue);
                    fpsqdglmap.put("djbh", rowList.get(0));
                    fpsqdglmap.put("khmc", rowList.get(2));
                    fpsqdglmap.put("khsh", rowList.get(3));
                    fpsqdglmap.put("khdzdh", rowList.get(4));
                    fpsqdglmap.put("khyhzh", rowList.get(5));
                    fpsqdglmap.put("khlxfs", rowList.get(6));
                    // 需兼容下旧的模板文件
                    // 27 28 29列是备用邮箱信息
                    if (rowList.size() > 29) {
                        fpsqdglmap.put("gmfEmail1", rowList.get(27));
                        fpsqdglmap.put("gmfEmail2", rowList.get(28));
                        fpsqdglmap.put("gmfEmail3", rowList.get(29));
                    } else {
                        fpsqdglmap.put("gmfEmail1", "");
                        fpsqdglmap.put("gmfEmail2", "");
                        fpsqdglmap.put("gmfEmail3", "");
                    }

                    if (rowList.size() > 30) {
                        String gmfZrrbs = StringUtilsExt.getString(rowList.get(30));

                        if (StringUtil.isEmpty(gmfZrrbs)) {
                            fpsqdglmap.put("gmfZrrbs", "N");
                        } else {
                            fpsqdglmap.put("gmfZrrbs", gmfZrrbs);
                        }
                    } else {
                        fpsqdglmap.put("gmfZrrbs", "N");
                    }

                    if (rowList.size() > 32) {
                        // 大额票
                        fpsqdglmap.put("sxedDefptxgz", StringUtilsExt.getString(rowList.get(32)));
                        // 销方开户行信息是否展示
                        fpsqdglmap.put("sfzsxsfyhzh", StringUtilsExt.getString(rowList.get(33)));
                        // 购方开户行信息是否展示
                        fpsqdglmap.put("sfzsgmfyhzh", StringUtilsExt.getString(rowList.get(34)));
                    }

                    fpsqdglmap.put("bz", rowList.get(19));
                    fpsqdglmap.put("kpr", rowList.get(20));
                    fpsqdglmap.put("skr", rowList.get(21));
                    fpsqdglmap.put("fhr", rowList.get(22));
                    fpsqdglmap.put("hsje", String.format("%.2f", hsje_sum_double));
                    fpsqdglmap.put("bhsje", String.format("%.2f", bhsje_sum_double));
                    fpsqdglmap.put("hjse", String.format("%.2f", hsje_sum_double - bhsje_sum_double));
                    fpsqdglmap.put("tax", rowList.get(13));
                    fpsqdglmap.put("sjly", excelDrMark);//br-导入类型标志  从excel导入
                    fpsqdglmap.put("fplxdm", fplxdm);//br-新增发票类型代码
                    fpsqdglmap.put("tspz", tspz);//br-新增特殊票种标识
                    fpsqdglmap.put("tdywlx", tdywlx);//全电票特定业务类型
                    fpsqdglmap.put("specialInfoJson", specialInfoJson);//全电票特定业务类型
                    fpsqdglmap.put("lrrq", dataStr);//br-新增录入日期
                    fpsqdglmap.put("djzt", "1");//br-新增单据状态
                    fpsqdglmap.put("kpzt", "0");//br-新增开票状态
                    fpsqdglmap.put("djrq", new Date());
                    fpsqdglmap.put("sjlx", sjlx);//rdd-数据类型
                    fpsqdglmap.put("kpfs", kpfs);//rdd-开票方式
                    fpsqdglmap.put("djhhTotal", djhhTotal + "");//单据行号总汇总
                    fpsqdglmap.put("sfwzp", sfwzp);

                    if (hsje_sum_double < 0) {
                        //红票
                        fpsqdglmap.put("kplx", "1");
                    } else if (hsje_sum_double > 0) {
                        //蓝票
                        fpsqdglmap.put("kplx", "0");
                    }
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(25))) {
                        if (StrUtil.equals("01", fplxdm) || StrUtil.equals("02", fplxdm)) {
                            fpsqdglmap.put("zsfs", zsfs);//征税方式
                        } else {
                            fpsqdglmap.put("zsfs", StringConstant.ZSFS_CEZS);//征税方式
                        }
                        fpsqdglmap.put("kce", rowList.get(25));//扣除额
                    }

                    fpsqdglmxmap.put("sqdmxid", StringUtilsExt.guidToStr());
                    fpsqdglmxmap.put("sqdid", uuidValue);
                    fpsqdglmxmap.put("djbh", rowList.get(0));
                    fpsqdglmxmap.put("spmc", rowList.get(7));
                    fpsqdglmxmap.put("ggxh", rowList.get(8));
                    fpsqdglmxmap.put("jldw", rowList.get(9));
                    fpsqdglmxmap.put("spsl", spsl_str);//br-商品数量
                    fpsqdglmxmap.put("hsje", String.format("%.2f", hsje_double));//br-合计金额
                    fpsqdglmxmap.put("bhsje", String.format("%.2f", bhsje_double));
                    fpsqdglmxmap.put("se", String.format("%.2f", se_double));
                    fpsqdglmxmap.put("hsdj", hsdj_str);//br-含税单价-rowList.get(11)
                    fpsqdglmxmap.put("bhsdj", bhsdj_str);
                    fpsqdglmxmap.put("tax", rowList.get(13));//br-税率

                    String ssbm = "";
                    String ssmc = "";
                    String spmc = "";
                    Map ssMap = new HashMap();
                    if (rowList.get(14) != null && rowList.get(14) != "") {
                        ssbm = rowList.get(14).toString();
                    }
                    if (rowList.get(7) != null && rowList.get(7) != "") {
                        spmc = rowList.get(7).toString();
                    }
                    if (StringUtils.isNotBlank(ssbm)) {
                        ssMap = getSsbm2(ssbm, tenant_id);
                        ssbm = (String) ssMap.get("spbm");
                        ssmc = (String) ssMap.get("ssmc");
                    }
                    if ("".equals(ssbm)) {
                        if (StringUtils.isNotBlank(spmc)) {
                            ssMap = getSsbm(spmc, tenant_id);
                            ssbm = (String) ssMap.get("spbm");
                            ssmc = (String) ssMap.get("ssmc");
                        }
                    }
                    fpsqdglmxmap.put("ssbm", ssbm);
                    fpsqdglmxmap.put("ssmc", ssmc);
                    fpsqdglmxmap.put("syyhzc", syyhzc);
                    fpsqdglmxmap.put("yhzc", yhzc);
                    fpsqdglmxmap.put("mslx", mslx);
                    fpsqdglmxmap.put("spdm", rowList.get(18));
                    fpsqdglmxmap.put("zkje", zkh_je_str);
                    fpsqdglmxmap.put("djhh", djhhValue + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增


                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                        fpsqdglmxmap.put("fphxz", "2");
                    } else {
                        fpsqdglmxmap.put("fphxz", "0");
                    }
                    if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                        fpsqdglmxmap.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                    }
                    fpsqdglList.add(fpsqdglmap);
                    fpsqdglMxList.add(fpsqdglmxmap);

                    //br-如果是有折扣行，紧接着在明细中再添加一行折扣行
                    if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                        zkhMap.put("sqdmxid", StringUtilsExt.guidToStr());
                        zkhMap.put("sqdid", uuidValue);
                        zkhMap.put("djbh", rowList.get(0));
                        zkhMap.put("spmc", rowList.get(7));
                        zkhMap.put("ggxh", "");//br-折扣行规格型号为空
                        zkhMap.put("jldw", "");//br-折扣行计量单位为空
                        zkhMap.put("spsl", "");//br-折扣行商品数量为空
                        zkhMap.put("hsdj", "");//br-折扣行含税单价为空
                        zkhMap.put("hsje", String.format("%.2f", zkh_hsje_double));//br-折扣行含税金额
                        zkhMap.put("tax", rowList.get(13));//br-折扣行税率如上
                        if (rowList.get(14) != null && rowList.get(14) != "") {
                            ssbm = rowList.get(14).toString();
                        } else if (rowList.get(7) != null && rowList.get(7) != "") {
                            spmc = rowList.get(7).toString();
                        }
                        if (StringUtils.isNotBlank(ssbm)) {
                            ssMap = getSsbm2(ssbm, tenant_id);
                            ssbm = (String) ssMap.get("spbm");
                            ssmc = (String) ssMap.get("ssmc");
                        }
                        if ("".equals(ssbm)) {
                            if (StringUtils.isNotBlank(spmc)) {
                                ssMap = getSsbm(spmc, tenant_id);
                                ssbm = (String) ssMap.get("spbm");
                                ssmc = (String) ssMap.get("ssmc");
                            }
                        }
                        zkhMap.put("ssbm", ssbm);
                        zkhMap.put("ssmc", ssmc);
                        zkhMap.put("syyhzc", syyhzc);
                        zkhMap.put("yhzc", yhzc);
                        zkhMap.put("mslx", mslx);
                        zkhMap.put("spdm", rowList.get(18));
                        zkhMap.put("bhsje", String.format("%.2f", zkh_bhsje_double));//br-折扣行不含税金额
                        zkhMap.put("se", String.format("%.2f", zkh_se_double));
                        zkhMap.put("djhh", djhhValue + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                        zkhMap.put("fphxz", "1");
                        if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                            zkhMap.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                        }
                        fpsqdglMxList.add(zkhMap);
                    }
                } else {
                    //这里是处理不是第一次添加的情况
                    Map<String, Object> fpsqdglmap01 = new HashMap<>();
                    Map<String, Object> fpsqdglmxmap01 = new HashMap<>();
                    Map<String, Object> zkhMap01 = new HashMap<>();
                    int countValue = fpsqdglList.size();//br-定义countValue,用以判断是否包含相应的字段的判断

                    for (int j = 0; j < fpsqdglList.size(); j++) {
                        Map<String, Object> fpsqdglmapValue = fpsqdglList.get(j);//br-遍历fpsqdglList和excel中取到的值进行比较

                        int djhhTotal = Integer.parseInt(fpsqdglmapValue.get("djhhTotal") + "");//br-单据行号-汇总，用于单据行号值的累加，每个单据的明细从1，依次递增
                        int djhhValue = Integer.parseInt(fpsqdglmapValue.get("djhhTotal") + "");//br-单据行号-当前值记录，用于单据行号值的累加，每个单据的明细从1，依次递增

                        /**
                         * br-计算主表的含税金额和不含税金额（需要累加各个明细的不含税金额的和）
                         */
                        String hsje_fpsqdglmap = "";
                        String bhsje_fpsqdglmap = "";
                        Double hsje_sum_double = null;
                        Double bhsje_sum_double = null;

                        if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) { //br-有折扣行的时候的操作，需要添加折扣行的数值
                            hsje_fpsqdglmap = fpsqdglmapValue.get("hsje").toString();//br-从临时存储的fpsqdglmapValue中暂时取出含税金额
                            bhsje_fpsqdglmap = fpsqdglmapValue.get("bhsje").toString();//br-从临时存储的fpsqdglmapValue中暂时取出不含税金额
                            hsje_sum_double = Double.parseDouble(String.format("%.2f", Double.parseDouble(hsje_fpsqdglmap) + hsje_double + zkh_hsje_double));//br-计算累加的含税金额
                            bhsje_sum_double = Double.parseDouble(String.format("%.2f", Double.parseDouble(bhsje_fpsqdglmap) + bhsje_double + zkh_bhsje_double));//br-计算累加的不含税金额
                            djhhTotal = djhhTotal + 1;
                        } else {//br-没有折扣行的时候直接累加即可
                            hsje_fpsqdglmap = fpsqdglmapValue.get("hsje").toString();//br-从临时存储的fpsqdglmapValue中暂时取出含税金额
                            bhsje_fpsqdglmap = fpsqdglmapValue.get("bhsje").toString();//br-从临时存储的fpsqdglmapValue中暂时取出不含税金额
                            hsje_sum_double = Double.parseDouble(String.format("%.2f", Double.parseDouble(hsje_fpsqdglmap) + hsje_double));//br-计算累加的含税金额
                            bhsje_sum_double = Double.parseDouble(String.format("%.2f", Double.parseDouble(bhsje_fpsqdglmap) + bhsje_double));//br-计算累加的不含税金额
                            djhhTotal = djhhTotal + 1;
                        }

                        if (org.springframework.util.StringUtils.isEmpty(rowList.get(0))) { //br-如果excel中取出来的单据编号是空的，再根据名称进行判断

                            int order = (int) fpsqdglmapValue.get("order") + 1;//br-按照新需求，用于记录添加的行数标记，取出标记，用来判断已经添加的数据，虽然名称相同，但是是不是紧挨着的数据
                            if (org.springframework.util.StringUtils.isEmpty(fpsqdglmapValue.get("djbh")) && fpsqdglmapValue.get("khmc").equals(rowList.get(2))
                                    && order == i && org.springframework.util.StringUtils.isEmpty(rowList.get(25)) && org.springframework.util.StringUtils.isEmpty(fpsqdglmapValue.get("kce"))) {
                                fpsqdglmapValue.put("order", i);//br-按照新需求,每次添加都要更新添加的order
                                fpsqdglmapValue.put("djhhTotal", djhhTotal);//br-单据行号汇总更新
                                fpsqdglmapValue.put("hsje", String.format("%.2f", hsje_sum_double));
                                fpsqdglmapValue.put("bhsje", String.format("%.2f", bhsje_sum_double));//br-更新fpsqdglmapValue中的值
                                fpsqdglmapValue.put("hjse", String.format("%.2f", hsje_sum_double - bhsje_sum_double));
                                fpsqdglmxmap01.put("sqdmxid", StringUtilsExt.guidToStr());
                                fpsqdglmxmap01.put("sqdid", fpsqdglmapValue.get("sqdid"));
                                fpsqdglmxmap01.put("spmc", rowList.get(7));
                                fpsqdglmxmap01.put("ggxh", rowList.get(8));
                                fpsqdglmxmap01.put("jldw", rowList.get(9));
                                fpsqdglmxmap01.put("spsl", spsl_str);
                                fpsqdglmxmap01.put("hsje", String.format("%.2f", hsje_double));
                                fpsqdglmxmap01.put("bhsje", String.format("%.2f", bhsje_double));
                                fpsqdglmxmap01.put("se", String.format("%.2f", se_double));
                                fpsqdglmxmap01.put("hsdj", hsdj_str);//br-含税单价-rowList.get(11)
                                fpsqdglmxmap01.put("bhsdj", bhsdj_str);
                                fpsqdglmxmap01.put("tax", rowList.get(13));
                                String ssbm = "";
                                String ssmc = "";
                                String spmc = "";
                                Map ssMap = new HashMap();
                                if (rowList.get(14) != null && rowList.get(14) != "") {
                                    ssbm = rowList.get(14).toString();
                                } else if (rowList.get(7) != null && rowList.get(7) != "") {
                                    spmc = rowList.get(7).toString();
                                }
                                if (StringUtils.isNotBlank(ssbm)) {
                                    ssMap = getSsbm2(ssbm, tenant_id);
                                    ssbm = (String) ssMap.get("spbm");
                                    ssmc = (String) ssMap.get("ssmc");
                                }
                                if ("".equals(ssbm)) {
                                    if (StringUtils.isNotBlank(spmc)) {
                                        ssMap = getSsbm(spmc, tenant_id);
                                        ssbm = (String) ssMap.get("spbm");
                                        ssmc = (String) ssMap.get("ssmc");
                                    }
                                }
                                fpsqdglmxmap01.put("ssbm", ssbm);
                                fpsqdglmxmap01.put("ssmc", ssmc);
                                fpsqdglmxmap01.put("syyhzc", syyhzc);
                                fpsqdglmxmap01.put("yhzc", yhzc);
                                fpsqdglmxmap01.put("mslx", mslx);
                                fpsqdglmxmap01.put("spdm", rowList.get(18));
                                fpsqdglmxmap01.put("djhh", djhhValue + 1 + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                                if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                                    fpsqdglmxmap01.put("fphxz", "2");
                                } else {
                                    fpsqdglmxmap01.put("fphxz", "0");
                                }
                                if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                                    fpsqdglmxmap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                                }
                                fpsqdglMxList.add(fpsqdglmxmap01);

                                if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) { //br-如果是有折扣行的话，然后紧接着还需要再添加一行折扣行
                                    zkhMap01.put("sqdmxid", StringUtilsExt.guidToStr());
                                    zkhMap01.put("sqdid", fpsqdglmapValue.get("sqdid"));
                                    zkhMap01.put("spmc", rowList.get(7));
                                    zkhMap01.put("ggxh", "");//br-折扣行规格型号为空
                                    zkhMap01.put("jldw", "");//br-折扣行计量单位为空
                                    zkhMap01.put("spsl", "");//br-折扣行商品数量为空
                                    zkhMap01.put("hsdj", "");//br-折扣行含税单价为空
                                    zkhMap01.put("hsje", String.format("%.2f", zkh_hsje_double));//br-折扣行含税金额
                                    zkhMap01.put("tax", rowList.get(13));//br-折扣行税率如上
                                    if (rowList.get(14) != null && rowList.get(14) != "") {
                                        ssbm = rowList.get(14).toString();
                                    } else if (rowList.get(7) != null && rowList.get(7) != "") {
                                        spmc = rowList.get(7).toString();
                                    }
                                    if (StringUtils.isNotBlank(ssbm)) {
                                        ssMap = getSsbm2(ssbm, tenant_id);
                                        ssbm = (String) ssMap.get("spbm");
                                        ssmc = (String) ssMap.get("ssmc");
                                    }
                                    if ("".equals(ssbm)) {
                                        if (StringUtils.isNotBlank(spmc)) {
                                            ssMap = getSsbm(spmc, tenant_id);
                                            ssbm = (String) ssMap.get("spbm");
                                            ssmc = (String) ssMap.get("ssmc");
                                        }
                                    }
                                    zkhMap01.put("ssbm", ssbm);
                                    zkhMap01.put("ssmc", ssmc);
                                    zkhMap01.put("syyhzc", syyhzc);
                                    zkhMap01.put("yhzc", yhzc);
                                    zkhMap01.put("mslx", mslx);
                                    zkhMap01.put("spdm", rowList.get(18));
                                    zkhMap01.put("bhsje", String.format("%.2f", zkh_bhsje_double));//br-折扣行不含税金额
                                    zkhMap01.put("se", String.format("%.2f", zkh_se_double));
                                    zkhMap01.put("djhh", djhhValue + 1 + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                                    zkhMap01.put("fphxz", "1");
                                    if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                                        zkhMap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                                    }
                                    fpsqdglMxList.add(zkhMap01);
                                }
                                break;
                            } else {
                                countValue = countValue - 1;     //br-若不存在，countValue减1
                            }
                        } else { //br-如果excel中取出来的单据编号不是空的，对比单据编号
                            if (fpsqdglmapValue.get("djbh").equals(rowList.get(0)) && org.springframework.util.StringUtils.isEmpty(rowList.get(25)) && org.springframework.util.StringUtils.isEmpty(fpsqdglmapValue.get("kce"))) {//若存在，继续下一次循环

                                fpsqdglmapValue.put("order", i);//br-按照新需求,每次添加都要更新添加的order
                                fpsqdglmapValue.put("djhhTotal", djhhTotal);//br-单据行号汇总更新
                                fpsqdglmapValue.put("hsje", String.format("%.2f", hsje_sum_double));
                                fpsqdglmapValue.put("bhsje", String.format("%.2f", bhsje_sum_double));//br-更新fpsqdglmapValue中的值
                                fpsqdglmapValue.put("hjse", String.format("%.2f", hsje_sum_double - bhsje_sum_double));
                                fpsqdglmxmap01.put("sqdmxid", StringUtilsExt.guidToStr());
                                fpsqdglmxmap01.put("sqdid", fpsqdglmapValue.get("sqdid"));
                                fpsqdglmxmap01.put("djbh", rowList.get(0));
                                fpsqdglmxmap01.put("spmc", rowList.get(7));
                                fpsqdglmxmap01.put("ggxh", rowList.get(8));
                                fpsqdglmxmap01.put("jldw", rowList.get(9));
                                fpsqdglmxmap01.put("spsl", spsl_str);
                                fpsqdglmxmap01.put("hsje", String.format("%.2f", hsje_double));
                                fpsqdglmxmap01.put("bhsje", String.format("%.2f", bhsje_double));
                                fpsqdglmxmap01.put("se", String.format("%.2f", se_double));
                                fpsqdglmxmap01.put("hsdj", hsdj_str);
                                fpsqdglmxmap01.put("bhsdj", bhsdj_str);
                                fpsqdglmxmap01.put("tax", rowList.get(13));
                                String ssbm = "";
                                String ssmc = "";
                                String spmc = "";
                                Map ssMap = new HashMap();
                                if (rowList.get(14) != null && rowList.get(14) != "") {
                                    ssbm = rowList.get(14).toString();
                                } else if (rowList.get(7) != null && rowList.get(7) != "") {
                                    spmc = rowList.get(7).toString();
                                }
                                if (StringUtils.isNotBlank(ssbm)) {
                                    ssMap = getSsbm2(ssbm, tenant_id);
                                    ssbm = (String) ssMap.get("spbm");
                                    ssmc = (String) ssMap.get("ssmc");
                                }
                                if ("".equals(ssbm)) {
                                    if (StringUtils.isNotBlank(spmc)) {
                                        ssMap = getSsbm(spmc, tenant_id);
                                        ssbm = (String) ssMap.get("spbm");
                                        ssmc = (String) ssMap.get("ssmc");
                                    }
                                }
                                fpsqdglmxmap01.put("ssbm", ssbm);
                                fpsqdglmxmap01.put("ssmc", ssmc);
                                fpsqdglmxmap01.put("syyhzc", syyhzc);
                                fpsqdglmxmap01.put("yhzc", yhzc);
                                fpsqdglmxmap01.put("mslx", mslx);
                                fpsqdglmxmap01.put("spdm", rowList.get(18));
                                fpsqdglmxmap01.put("djhh", djhhValue + 1 + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                                if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                                    fpsqdglmxmap01.put("fphxz", "2");
                                } else {
                                    fpsqdglmxmap01.put("fphxz", "0");
                                }
                                if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                                    fpsqdglmxmap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                                }
                                fpsqdglMxList.add(fpsqdglmxmap01);

                                if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) { //br-如果是有折扣行的话，然后紧接着还需要再添加一行折扣行
                                    zkhMap01.put("sqdmxid", StringUtilsExt.guidToStr());
                                    zkhMap01.put("sqdid", fpsqdglmapValue.get("sqdid"));
                                    zkhMap01.put("djbh", rowList.get(0));
                                    zkhMap01.put("spmc", rowList.get(7));
                                    zkhMap01.put("ggxh", "");//br-折扣行规格型号为空
                                    zkhMap01.put("jldw", "");//br-折扣行计量单位为空
                                    zkhMap01.put("spsl", "");//br-折扣行商品数量为空
                                    zkhMap01.put("hsdj", "");//br-折扣行含税单价为空
                                    zkhMap01.put("hsje", String.format("%.2f", zkh_hsje_double));//br-折扣行含税金额
                                    zkhMap01.put("tax", rowList.get(13));//br-折扣行税率如上
                                    if (rowList.get(14) != null && rowList.get(14) != "") {
                                        ssbm = rowList.get(14).toString();
                                    } else if (rowList.get(7) != null && rowList.get(7) != "") {
                                        spmc = rowList.get(7).toString();
                                    }
                                    if (StringUtils.isNotBlank(ssbm)) {
                                        ssMap = getSsbm2(ssbm, tenant_id);
                                        ssbm = (String) ssMap.get("spbm");
                                        ssmc = (String) ssMap.get("ssmc");
                                    }
                                    if ("".equals(ssbm)) {
                                        if (StringUtils.isNotBlank(spmc)) {
                                            ssMap = getSsbm(spmc, tenant_id);
                                            ssbm = (String) ssMap.get("spbm");
                                            ssmc = (String) ssMap.get("ssmc");
                                        }
                                    }
                                    zkhMap01.put("ssbm", ssbm);
                                    zkhMap01.put("ssmc", ssmc);
                                    zkhMap01.put("syyhzc", syyhzc);
                                    zkhMap01.put("yhzc", yhzc);
                                    zkhMap01.put("mslx", mslx);
                                    zkhMap01.put("spdm", rowList.get(18));
                                    zkhMap01.put("bhsje", String.format("%.2f", zkh_bhsje_double));//br-折扣行不含税金额
                                    zkhMap01.put("se", String.format("%.2f", zkh_se_double));
                                    zkhMap01.put("djhh", djhhValue + 1 + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                                    zkhMap01.put("fphxz", "1");
                                    if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                                        zkhMap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                                    }
                                    fpsqdglMxList.add(zkhMap01);
                                }
                                break;
                            } else {
                                countValue = countValue - 1;     //br-若不存在，countValue减1
                            }
                        }
                    }
                    // 如果countValue等于零，说明将要插入的值没有重复的
                    if (countValue == 0) {
                        int djhhValue = 1;//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                        int djhhTotal = 0;

                        Double hsje_sum_double = null;
                        Double bhsje_sum_double = null;
                        if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {//br-有折扣行的需要在极速验主表的含税接的时候添加上折扣行金额
                            hsje_sum_double = Double.parseDouble(String.format("%.2f", hsje_double + zkh_hsje_double));//br-计算保存到主表的“含税金额”的总和
                            bhsje_sum_double = Double.parseDouble(String.format("%.2f", bhsje_double + zkh_bhsje_double));//br-计算保存到主表的“不含税金额”的总和
                            djhhTotal = djhhValue + 1;
                        } else {
                            //br-没有折扣行的时候正常添加
                            hsje_sum_double = hsje_double;//br-计算保存到主表的“含税金额”的总和
                            bhsje_sum_double = bhsje_double;//br-计算保存到主表的“不含税金额”的总和
                            djhhTotal = djhhValue;
                        }

                        djbh = (String) rowList.get(0);
                        String specialInfoJson = getSpecialInfoJson(djbh, tdywMap);

                        String tdywlx = getTdywlxDm(djbh, tdywMap);

                        String uuidValue = IdWorker.getIdStr();
                        fpsqdglmap01.put("order", i);//br-按照新需求，用于记录添加的行数标记，用于判断无订单编号名称相同，是否是紧挨着的几个
                        fpsqdglmap01.put("sqdid", uuidValue);
                        fpsqdglmap01.put("djbh", djbh);
                        fpsqdglmap01.put("khmc", rowList.get(2));
                        fpsqdglmap01.put("khsh", rowList.get(3));
                        fpsqdglmap01.put("khdzdh", rowList.get(4));
                        fpsqdglmap01.put("khyhzh", rowList.get(5));
                        fpsqdglmap01.put("khlxfs", rowList.get(6));
                        fpsqdglmap01.put("bz", rowList.get(19));
                        fpsqdglmap01.put("kpr", rowList.get(20));
                        fpsqdglmap01.put("skr", rowList.get(21));
                        fpsqdglmap01.put("fhr", rowList.get(22));
                        fpsqdglmap01.put("hsje", String.format("%.2f", hsje_sum_double));
                        fpsqdglmap01.put("bhsje", String.format("%.2f", bhsje_sum_double));
                        fpsqdglmap01.put("hjse", String.format("%.2f", hsje_sum_double - bhsje_sum_double));
                        fpsqdglmap01.put("tax", rowList.get(13));
                        fpsqdglmap01.put("sjly", excelDrMark);//br-导入类型标志
                        fpsqdglmap01.put("fplxdm", fplxdm);//br-新增发票类型代码
                        fpsqdglmap01.put("tspz", tspz);//br-新增特殊票种标识
                        fpsqdglmap01.put("tdywlx", tdywlx);//全电票特定业务类型
                        fpsqdglmap01.put("specialInfoJson", specialInfoJson);//全电票特定业务类型
                        fpsqdglmap01.put("lrrq", dataStr);//br-新增录入日期
                        fpsqdglmap01.put("djzt", "1");//br-新增单据状态
                        fpsqdglmap01.put("kpzt", "0");//br-新增开票状态
                        fpsqdglmap01.put("djrq", new Date());
                        fpsqdglmap01.put("sjlx", sjlx);//rdd-数据类型
                        fpsqdglmap01.put("kpfs", kpfs);//rdd-开票方式
                        fpsqdglmap01.put("djhhTotal", djhhTotal + "");//单据行号总汇总
                        fpsqdglmap01.put("sfwzp", sfwzp);

                        // 需兼容下旧的模板文件
                        // 27 28 29列是备用邮箱信息
                        if (rowList.size() > 29) {
                            fpsqdglmap01.put("gmfEmail1", rowList.get(27));
                            fpsqdglmap01.put("gmfEmail2", rowList.get(28));
                            fpsqdglmap01.put("gmfEmail3", rowList.get(29));
                        } else {
                            fpsqdglmap01.put("gmfEmail1", "");
                            fpsqdglmap01.put("gmfEmail2", "");
                            fpsqdglmap01.put("gmfEmail3", "");
                        }

                        if (rowList.size() > 30) {
                            String gmfZrrbs = StringUtilsExt.getString(rowList.get(30));

                            if (StringUtil.isEmpty(gmfZrrbs)) {
                                fpsqdglmap01.put("gmfZrrbs", "N");
                            } else {
                                fpsqdglmap01.put("gmfZrrbs", gmfZrrbs);
                            }
                        } else {
                            fpsqdglmap01.put("gmfZrrbs", "N");
                        }

                        if (rowList.size() > 32) {
                            // 大额票
                            fpsqdglmap01.put("sxedDefptxgz", StringUtilsExt.getString(rowList.get(32)));
                            // 销方开户行信息是否展示
                            fpsqdglmap01.put("sfzsxsfyhzh", StringUtilsExt.getString(rowList.get(33)));
                            // 购方开户行信息是否展示
                            fpsqdglmap01.put("sfzsgmfyhzh", StringUtilsExt.getString(rowList.get(34)));
                        }

                        if (hsje_sum_double < 0) {
                            fpsqdglmap01.put("kplx", "1");
                        } else if (hsje_sum_double > 0) {
                            fpsqdglmap01.put("kplx", "0");
                        }
                        if (!org.springframework.util.StringUtils.isEmpty(rowList.get(25))) {
                            if (StrUtil.equals("01", fplxdm) || StrUtil.equals("02", fplxdm)) {
                                fpsqdglmap01.put("zsfs", zsfs);//征税方式
                            } else {
                                fpsqdglmap01.put("zsfs", StringConstant.ZSFS_CEZS);//征税方式
                            }
                            fpsqdglmap01.put("kce", rowList.get(25));//扣除额
                        }
                        fpsqdglmxmap01.put("sqdmxid", StringUtilsExt.guidToStr());
                        fpsqdglmxmap01.put("sqdid", uuidValue);
                        fpsqdglmxmap01.put("djbh", rowList.get(0));
                        fpsqdglmxmap01.put("spmc", rowList.get(7));
                        fpsqdglmxmap01.put("ggxh", rowList.get(8));
                        fpsqdglmxmap01.put("jldw", rowList.get(9));
                        fpsqdglmxmap01.put("spsl", spsl_str);
                        fpsqdglmxmap01.put("hsje", String.format("%.2f", hsje_double));
                        fpsqdglmxmap01.put("bhsje", String.format("%.2f", bhsje_double));
                        fpsqdglmxmap01.put("se", String.format("%.2f", se_double));
                        fpsqdglmxmap01.put("hsdj", hsdj_str);
                        fpsqdglmxmap01.put("bhsdj", bhsdj_str);
                        fpsqdglmxmap01.put("tax", rowList.get(13));
                        String ssbm = "";
                        String ssmc = "";
                        String spmc = "";
                        Map ssMap = new HashMap();
                        if (rowList.get(14) != null && rowList.get(14) != "") {
                            ssbm = rowList.get(14).toString();
                        } else if (rowList.get(7) != null && rowList.get(7) != "") {
                            spmc = rowList.get(7).toString();
                        }
                        if (StringUtils.isNotBlank(spmc)) {
                            ssMap = getSsbm(spmc, tenant_id);
                            ssbm = (String) ssMap.get("spbm");
                            ssmc = (String) ssMap.get("ssmc");
                        }
                        if (StringUtils.isNotBlank(ssbm)) {
                            ssMap = getSsbm2(ssbm, tenant_id);
                            ssbm = (String) ssMap.get("spbm");
                            ssmc = (String) ssMap.get("ssmc");
                        }
                        fpsqdglmxmap01.put("ssbm", ssbm);
                        fpsqdglmxmap01.put("ssmc", ssmc);
                        fpsqdglmxmap01.put("syyhzc", syyhzc);
                        fpsqdglmxmap01.put("yhzc", yhzc);
                        fpsqdglmxmap01.put("mslx", mslx);
                        fpsqdglmxmap01.put("spdm", rowList.get(18));
                        fpsqdglmxmap01.put("djhh", djhhValue + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                        if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {
                            fpsqdglmxmap01.put("fphxz", "2");
                        } else {
                            fpsqdglmxmap01.put("fphxz", "0");
                        }

                        if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                            fpsqdglmxmap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                        }

                        fpsqdglList.add(fpsqdglmap01);
                        fpsqdglMxList.add(fpsqdglmxmap01);

                        if (!org.springframework.util.StringUtils.isEmpty(rowList.get(23)) || (rowList.size() > 35 && !org.springframework.util.StringUtils.isEmpty(rowList.get(35)))) {//br-如果是有折扣行，紧接着在明细中再添加一行折扣行
                            zkhMap01.put("sqdmxid", StringUtilsExt.guidToStr());
                            zkhMap01.put("sqdid", uuidValue);
                            zkhMap01.put("djbh", rowList.get(0));
                            zkhMap01.put("spmc", rowList.get(7));
                            zkhMap01.put("ggxh", "");//br-折扣行规格型号为空
                            zkhMap01.put("jldw", "");//br-折扣行计量单位为空
                            zkhMap01.put("spsl", "");//br-折扣行商品数量为空
                            zkhMap01.put("hsdj", "");//br-折扣行含税单价为空
                            zkhMap01.put("hsje", String.format("%.2f", zkh_hsje_double));//br-折扣行含税金额
                            zkhMap01.put("tax", rowList.get(13));//br-折扣行税率如上
                            if (rowList.get(14) != null && rowList.get(14) != "") {
                                ssbm = rowList.get(14).toString();
                            } else if (rowList.get(7) != null && rowList.get(7) != "") {
                                spmc = rowList.get(7).toString();
                            }
                            if (StringUtils.isNotBlank(spmc)) {
                                ssMap = getSsbm(spmc, tenant_id);
                                ssbm = (String) ssMap.get("spbm");
                                ssmc = (String) ssMap.get("ssmc");
                            }
                            if (StringUtils.isNotBlank(ssbm)) {
                                ssMap = getSsbm2(ssbm, tenant_id);
                                ssbm = (String) ssMap.get("spbm");
                                ssmc = (String) ssMap.get("ssmc");
                            }
                            zkhMap01.put("ssbm", ssbm);
                            zkhMap01.put("ssmc", ssmc);
                            zkhMap01.put("syyhzc", syyhzc);
                            zkhMap01.put("yhzc", yhzc);
                            zkhMap01.put("mslx", mslx);
                            zkhMap01.put("spdm", rowList.get(18));
                            zkhMap01.put("bhsje", String.format("%.2f", zkh_bhsje_double));//br-折扣行不含税金额
                            zkhMap01.put("se", String.format("%.2f", zkh_se_double));
                            zkhMap01.put("djhh", djhhValue + "");//br-单据行号-用于单据行号值的累加，每个单据的明细从1，依次递增
                            zkhMap01.put("fphxz", "1");

                            if (rowList.size() > 31 && org.apache.commons.lang3.StringUtils.isNotBlank(StringUtilsExt.getString(rowList.get(31)))) {
                                zkhMap01.put("sppc", StringUtilsExt.getString(rowList.get(31)));
                            }
                            fpsqdglMxList.add(zkhMap01);
                        }
                    }
                }
            }

            for (int i = 0; i < fpsqdglList.size(); i++) {
                Map<String, Object> sqdMap = fpsqdglList.get(i);
                //根据纳税人识别号 ，客户名称活客户税号，补全 客户信息
                //客户名称
                String khdw = (String) sqdMap.get("khmc");
                //客户税号
                String nsrsbh = (String) sqdMap.get("khsh");
                if (!org.springframework.util.StringUtils.isEmpty(sqdMap.get("khmc")) || !org.springframework.util.StringUtils.isEmpty(sqdMap.get("khsh"))) {
                    List<CsXxCustom> custom = iCsXxCustomService.lambdaQuery().eq(StringUtils.isNotEmpty(khdw), CsXxCustom::getKhdw, khdw).eq(StringUtils.isNotEmpty(nsrsbh), CsXxCustom::getNsrsbh, nsrsbh).list();
                    if (!custom.isEmpty()) {
                        //客户信息不为空  赋值将为空的数据项
                        CsXxCustom csXxCustom = custom.get(0);
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("khmc"))) {
                            sqdMap.remove("khmc");
                            sqdMap.put("khmc", csXxCustom.getKhdw());
                        }
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("khsh"))) {
                            sqdMap.remove("khsh");
                            sqdMap.put("khsh", csXxCustom.getNsrsbh());
                        }
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("khdzdh"))) {
                            sqdMap.remove("khdzdh");
                            sqdMap.put("khdzdh", csXxCustom.getLxdz() + " " + csXxCustom.getLxdh());
                            sqdMap.put("khdz", csXxCustom.getLxdz());
                            sqdMap.put("khdh", csXxCustom.getLxdh());
                            sqdMap.put("gmf_mobile", csXxCustom.getLxdh());
                        }
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("khlxfs"))) {
                            sqdMap.remove("khlxfs");
                            sqdMap.put("khlxfs", csXxCustom.getLxdh());
                            sqdMap.put("gmf_email", csXxCustom.getMryx());
                        }
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("gmf_email"))) {
                            sqdMap.remove("gmf_email");
                            sqdMap.put("gmf_email", csXxCustom.getMryx());
                        }
                        if (org.springframework.util.StringUtils.isEmpty(sqdMap.get("khyhzh"))) {
                            sqdMap.remove("khyhzh");
                            sqdMap.put("khyhzh", csXxCustom.getKhhzh());
                            sqdMap.put("khkhhmc", csXxCustom.getKhhmc());
                        }
                        sqdMap.put("khdm", csXxCustom.getKhdm());
                    }
                }
            }

            Map<String, Object> listMap = new HashMap<>();
            listMap.put("fpsqdglList", fpsqdglList);
            listMap.put("fpsqdglMxList", fpsqdglMxList);
            listMap.put("djdr", "1");//单据导入标识
            insertFpsqdglAndMxDrMap1(listMap, judgeHanshui);
        }

        if (!"".equals(msg)) {
            throw new JeecgBootException(msg);
        }
        return FileValue;
    }


    public synchronized int insertFpsqdglAndMxDrMap1(Map listMap, String hsbz) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Integer tenant_id = user.getLoginTenantId();
        String company_id = user.getCompanyId();
        String sys_org_code = user.getOrgCode();
        //当前登录人的企业信息
        CsPtQyxx qyxx = LoginUserUtil.getQyxxByLoginUser();
        String xfmc = qyxx.getNsrmc();
        String xfsh = qyxx.getNsrsbh();

        List<Map<String, Object>> fpsqdglList = null;
        fpsqdglList = (ArrayList) listMap.get("fpsqdglList");
        List<Map<String, Object>> fpsqdglMxList = (ArrayList) listMap.get("fpsqdglMxList");

        if (fpsqdglMxList.isEmpty()) {
            return -1;
        }
        List<String> sqdIdList = new ArrayList<>();
        //插入待开单明细表
        for (int i = 0; i < fpsqdglMxList.size(); i++) {
            Map<String, Object> mxMap = fpsqdglMxList.get(i);
            CsXxDkdjmx csXxDkdjmx = new CsXxDkdjmx();
            csXxDkdjmx.setId(Convert.toStr(mxMap.get("sqdmxid")));
            csXxDkdjmx.setTenantId(tenant_id);
            csXxDkdjmx.setCompanyId(company_id);
            csXxDkdjmx.setFromid(Convert.toStr(mxMap.get("sqdid")));
            csXxDkdjmx.setYwdjbh(Convert.toStr(mxMap.get("djbh")));
            csXxDkdjmx.setMxhh(Integer.parseInt(Convert.toStr(mxMap.get("djhh"), "0")));
            csXxDkdjmx.setSpdm(Convert.toStr(mxMap.get("spdm")));
            csXxDkdjmx.setSpmc(Convert.toStr(mxMap.get("spmc")));
            csXxDkdjmx.setGgxh(Convert.toStr(mxMap.get("ggxh")));
            csXxDkdjmx.setJldw(Convert.toStr(mxMap.get("jldw")));
            csXxDkdjmx.setSpsl(Convert.toBigDecimal(mxMap.get("spsl"), BigDecimal.ZERO));
            csXxDkdjmx.setHsdj(Convert.toBigDecimal(mxMap.get("hsdj"), BigDecimal.ZERO));
            csXxDkdjmx.setHsje(Convert.toBigDecimal(mxMap.get("hsje"), BigDecimal.ZERO));
            csXxDkdjmx.setBhsdj(Convert.toBigDecimal(mxMap.get("bhsdj"), BigDecimal.ZERO));
            csXxDkdjmx.setBhsje(Convert.toBigDecimal(mxMap.get("bhsje"), BigDecimal.ZERO));
            csXxDkdjmx.setTax(Convert.toBigDecimal(mxMap.get("tax"), BigDecimal.ZERO));
            csXxDkdjmx.setTaxje(Convert.toBigDecimal(mxMap.get("se"), BigDecimal.ZERO));
            csXxDkdjmx.setZkje(Convert.toBigDecimal(mxMap.get("zkje"), BigDecimal.ZERO));
            csXxDkdjmx.setSpbm(Convert.toStr(mxMap.get("ssbm")));
            csXxDkdjmx.setSsmc(Convert.toStr(mxMap.get("ssmc")));
            csXxDkdjmx.setLslbs(Convert.toStr(mxMap.get("mslx")));
            csXxDkdjmx.setYhzcbs(Convert.toStr(mxMap.get("yhzc")));
            csXxDkdjmx.setZzstsgl("");
            csXxDkdjmx.setFphxz(Convert.toStr(mxMap.get("fphxz")));
            iCsXxDkdjmxService.save(csXxDkdjmx);
        }

        //插入待开单主表
        for (int j = 0; j < fpsqdglList.size(); j++) {
            Map<String, Object> dkdjMap = fpsqdglList.get(j);
            CsXxDkdj csXxDkdj = new CsXxDkdj();
            csXxDkdj.setId(Convert.toStr(dkdjMap.get("sqdid"), ""));
            csXxDkdj.setTenantId(tenant_id);
            csXxDkdj.setSysOrgCode(sys_org_code);
            csXxDkdj.setCompanyId(company_id);
            csXxDkdj.setSjlx(Convert.toStr(dkdjMap.get("sjlx"), ""));//数据类型
            csXxDkdj.setSjyt("");//数据业态
            csXxDkdj.setSjly("djdr");//数据来源
            csXxDkdj.setDjlx("");//单据类型
            csXxDkdj.setClfs("0");//单据处理方式 拆分合并之类的方式  0为默认值
            csXxDkdj.setKplx(Convert.toStr(dkdjMap.get("kplx"), ""));//开票类型
            csXxDkdj.setYwdjbh(Convert.toStr(dkdjMap.get("djbh"), ""));
            csXxDkdj.setDjrq(new Date());
            csXxDkdj.setKhdm(Convert.toStr(dkdjMap.get("khdm"), ""));
            csXxDkdj.setKhmc(Convert.toStr(dkdjMap.get("khmc"), ""));
            csXxDkdj.setKhsh(Convert.toStr(dkdjMap.get("khsh"), ""));
            csXxDkdj.setKhdz(Convert.toStr(dkdjMap.get("khdz"), ""));
            csXxDkdj.setKhdh(Convert.toStr(dkdjMap.get("khdh"), ""));
            csXxDkdj.setKhyh(Convert.toStr(dkdjMap.get("khkhhmc"), ""));
            csXxDkdj.setKhzh(Convert.toStr(dkdjMap.get("khyhzh"), ""));
            csXxDkdj.setFplxdm(Convert.toStr(dkdjMap.get("fplxdm"), ""));
            csXxDkdj.setTspz(Convert.toStr(dkdjMap.get("tspz"), ""));
            csXxDkdj.setZsfs(Convert.toStr(dkdjMap.get("zsfs"), "0"));
            csXxDkdj.setKhlxyx(Convert.toStr(dkdjMap.get("gmf_email"), ""));
            csXxDkdj.setKhlxfs(Convert.toStr(dkdjMap.get("gmf_mobile"), ""));
            csXxDkdj.setYjdz("");//邮寄地址
            csXxDkdj.setYjjsr("");//邮寄接收人
            csXxDkdj.setKpzddm("");//开票终端代码
            csXxDkdj.setGsdm(Convert.toStr(qyxx.getNsrdm(), ""));//公司代码
            csXxDkdj.setXfmc(Convert.toStr(xfmc, ""));
            csXxDkdj.setXfsh(Convert.toStr(xfsh, ""));
            csXxDkdj.setYhdm(Convert.toStr(user.getUsername(), ""));
            csXxDkdj.setKpr(Convert.toStr(dkdjMap.get("kpr"), ""));
            csXxDkdj.setSkr(Convert.toStr(dkdjMap.get("skr") + ""));
            csXxDkdj.setFhr(Convert.toStr(dkdjMap.get("fhr"), ""));
            csXxDkdj.setBz(Convert.toStr(dkdjMap.get("bz"), ""));
            csXxDkdj.setHsbz(hsbz);
            csXxDkdj.setHjje(Convert.toBigDecimal(dkdjMap.get("bhsje"), BigDecimal.ZERO));
            csXxDkdj.setHjse(Convert.toBigDecimal(dkdjMap.get("hjse"), BigDecimal.ZERO));
            csXxDkdj.setZkje(Convert.toBigDecimal(dkdjMap.get("zkje"), BigDecimal.ZERO));
            csXxDkdj.setZdkpbz("");//自动开票标志
            //开票状态  0-未开票  1-已开票  2-部分开票 3-开票中 4-开票失败 5.已作废 7.全部红冲
            csXxDkdj.setKpzt("0");
            csXxDkdj.setDjzt("0");//单据状态 0有效 1无效
            csXxDkdj.setZzfpbz(Convert.toStr(dkdjMap.get("sfwzp"), ""));//是否数电纸票 0不是 1是
            iCsXxDkdjService.save(csXxDkdj);

            //数电特定业务 json
            String specialInfoJson = Convert.toStr(dkdjMap.get("specialInfoJson"), "");
            if (!"".equals(specialInfoJson)) {
                CsXxDkdjExt csXxDkdjExt = new CsXxDkdjExt();
                csXxDkdjExt.setId(IdWorker.getIdStr());
                csXxDkdjExt.setCompanyId(company_id);
                csXxDkdjExt.setTenantId(tenant_id);
                csXxDkdjExt.setTdywInfo(specialInfoJson);
                csXxDkdjExt.setFromid(Convert.toStr(dkdjMap.get("sqdid"), ""));
                iCsXxDkdjExtService.save(csXxDkdjExt);
            }
        }
        return 0;
    }


    public Map getSsbm(String spmc, String TenantId) {
        Map returnMap = new HashMap();
        String spbm = "";
        String ssmc = "";
        LambdaQueryWrapper<CsXxProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CsXxProduct::getSpmc, spmc);
        queryWrapper.eq(CsXxProduct::getTenantId, TenantId);
        queryWrapper.orderByAsc(CsXxProduct::getSpbm);
        List<CsXxProduct> list = iCsXxProductService.list(queryWrapper);
        if (!list.isEmpty()) {
            spbm = list.get(0).getSpbm();
            ssmc = list.get(0).getSpssjc();
        }
        returnMap.put("spbm", spbm);
        returnMap.put("ssmc", ssmc);
        return returnMap;
    }

    private Map getSsbm2(String spbm, String TenantId) {
        Map returnMap = new HashMap();
        String ssmc = "";
        if (StringUtil.isNotEmpty(spbm) && StringUtil.isNotEmpty(TenantId)) {
            List<CsXxProduct> list = iCsXxProductService.lambdaQuery().eq(CsXxProduct::getSpbm, spbm).eq(CsXxProduct::getTenantId, TenantId).list();
            if (!list.isEmpty()) {
                spbm = list.get(0).getSpbm();
                ssmc = list.get(0).getSpssjc();
            }
        }
        returnMap.put("spbm", spbm);
        returnMap.put("ssmc", ssmc);
        return returnMap;
    }

    private String getSpecialInfoJson(String djbh, Map<String, List<Tdywlx>> tdywMap) {
        String specialInfoJson = null;
        List<Tdywlx> tdywlxList = tdywMap.get(djbh);
        if (tdywlxList != null && !tdywlxList.isEmpty()) {
            Tdywlx tdyw = tdywlxList.get(0);
            if (org.apache.commons.lang3.StringUtils.equals(tdyw.getDataType(), "object")) {
                specialInfoJson = JSON.toJSONString(tdyw);
            } else {
                if (ReflectUtil.hasField(tdyw.getClass(), "xh")) {
                    for (int i = 0; i < tdywlxList.size(); i++) {
                        Tdywlx tdywlx = tdywlxList.get(i);
                        ReflectUtil.setFieldValue(tdywlx, "xh", String.valueOf(i + 1));
                    }
                }
                specialInfoJson = JSON.toJSONString(tdywlxList);
            }
        }
        return specialInfoJson;
    }

    private String getTdywlxDm(String djbh, Map<String, List<Tdywlx>> tdywMap) {
        List<Tdywlx> tdywlxList = tdywMap.get(djbh);
        if (tdywlxList != null && !tdywlxList.isEmpty()) {
            Tdywlx tdyw = tdywlxList.get(0);
            return tdyw.getTdywlxDm();
        }
        return null;
    }


    private void validateCepzmx(List<CepzmxVo> cepzmxVoList) {
        for (CepzmxVo cepzmxVo : cepzmxVoList) {
            Ret<String> response = cepzmxVo.validate(cepzmxVo);
            if (0 != response.getCode()) {
                throw new JeecgBootException("开具差额征收票失败！" + response.getRenson());

            }
            Ret<String> ret = cepzmxVo.selfCheck();
            if (0 != ret.getCode()) {
                throw new JeecgBootException("开具差额征收票失败！" + response.getRenson());
            }
        }
    }


    private Map<String, List<Tdywlx>> readSpecialInfoJson(MultipartFile file, String fplxdm) {
        ImportParams params = new ImportParams();
        params.setStartSheetIndex(1);
        List<Jzfw> jzfwList = ExcelUtils.importExcel(file, 0, 1, Jzfw.class, params);
        params.setStartSheetIndex(2);
        List<Bdcjyzlfw> bdcjyzlfwList = ExcelUtils.importExcel(file, 0, 1, Bdcjyzlfw.class, params);
        params.setStartSheetIndex(3);
        List<Bdcxsfw> bdcxsfwList = ExcelUtils.importExcel(file, 0, 1, Bdcxsfw.class, params);
        params.setStartSheetIndex(4);
        List<Hwysfwmx> hwysfwmxList = ExcelUtils.importExcel(file, 0, 1, Hwysfwmx.class, params);
        params.setStartSheetIndex(5);
        List<Lkysfwmx> lkysfwmxList = ExcelUtils.importExcel(file, 0, 1, Lkysfwmx.class, params);

        List<Tdywlx> tdywlxList = new ArrayList<>();
        if (jzfwList != null) {
            jzfwList.forEach(item -> {
                item.setJzfwfsd(item.getJzfwfsd() + item.getJzfwfsdxxdz());
            });
            tdywlxList.addAll(jzfwList);
        }
        if (bdcjyzlfwList != null) {
            bdcjyzlfwList.forEach(item -> {
                if (StrUtil.equals("01", fplxdm) || StrUtil.equals("02", fplxdm)) {
                    item.setBdcdz(item.getBdcdz());
                } else {
                    item.setBdcdz(item.getBdcdz() + item.getBdcdzxxdz());
                }
            });
            tdywlxList.addAll(bdcjyzlfwList);
        }
        if (bdcxsfwList != null) {
            bdcxsfwList.forEach(item -> {
                item.setBdcdz(item.getBdcdz() + item.getBdcdzxxdz());
            });
            tdywlxList.addAll(bdcxsfwList);
        }
        if (hwysfwmxList != null) {
            tdywlxList.addAll(hwysfwmxList);
        }
        if (lkysfwmxList != null) {
            tdywlxList.addAll(lkysfwmxList);
        }

        tdywlxList = ObjectUtils.filterEmpty(tdywlxList);
        return tdywlxList.stream().filter(map -> map.getDjbh() != null).collect(Collectors.groupingBy(Tdywlx::getDjbh));
    }

    private Map<String, List<CepzmxVo>> readCepzmx(MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setStartSheetIndex(6);
        List<CepzmxVo> cepzmxVoList = ExcelUtils.importExcel(file, 0, 1, CepzmxVo.class, params);
        Map<String, List<CepzmxVo>> result = new HashMap<>();
        if (CollUtil.isNotEmpty(cepzmxVoList)) {
            result = cepzmxVoList.stream().filter(item -> item.getDjbh() != null).collect(Collectors.groupingBy(CepzmxVo::getDjbh));
            if (CollUtil.isNotEmpty(result)) {
                for (String key : result.keySet()) {
                    List<CepzmxVo> cepzmxVos = result.get(key);
                    for (int i = 0; i < cepzmxVos.size(); i++) {
                        CepzmxVo cepzmxVo = cepzmxVos.get(i);
                        cepzmxVo.setXh(String.valueOf(i + 1));
                        if (i > 0) {
                            cepzmxVo.setKce(cepzmxVo.getBckcje().add(cepzmxVoList.get(i - 1).getKce()));
                        } else {
                            cepzmxVo.setKce(cepzmxVo.getBckcje());
                        }
                        cepzmxVo.setHjje(cepzmxVo.getPzhjje());
                        cepzmxVo.setLy(PzlyEnum.SGLR.getValue());
                        //转换凭证类型
                        PzlxEnum pzlxEnum = PzlxEnum.getDataByName(cepzmxVo.getPzlx());
                        if (ObjectUtil.isNotNull(pzlxEnum)) {
                            cepzmxVo.setPzlx(pzlxEnum.getValue());
                        } else {
                            //获取到了非法的凭证类型，设置为空，统一校验
                            cepzmxVo.setPzlx(null);
                        }
                    }
                }
            }
        }
        return result;
    }

    public static List<Object> getAllExcelRowFpsqdgl(File file, int sheetIndex, int startIndex) throws IOException {
        ArrayList<Object> lRowObj = new ArrayList<>();
        Workbook workbook = null;

        try {
            workbook = new HSSFWorkbook(new FileInputStream(file));
        } catch (Exception ex) {
            workbook = new XSSFWorkbook(new FileInputStream(file));
        }

        try {
            int index = sheetIndex < 0 ? 0 : sheetIndex;
            startIndex = startIndex < 0 ? 1 : startIndex;
            Sheet sheet = null;
            Row row = null;
            Cell cell = null;
            // 取得第index个Sheet
            sheet = workbook.getSheetAt(index);
            int rowNum = sheet.getLastRowNum();
            row = sheet.getRow(0);
            int colNum = row.getPhysicalNumberOfCells();

            // 正文内容应该从开始行索引开始
            for (int i = startIndex - 1; i <= rowNum; i++) {
                row = sheet.getRow(i);
                if (row != null) {//对于获取的row可能为null的情况进行处理，防止出现空指针异常
                    int j = 0;
                    int m = 0;
                    ArrayList<String> lCellStr = new ArrayList<>();
                    while (j < colNum) {
                        cell = row.getCell(j);
                        if (cell != null) {
                            String value;
                            if (cell.getCellType() == CellType.NUMERIC) {
                                value = new BigDecimal(String.valueOf(cell.getNumericCellValue())).stripTrailingZeros().toPlainString();
                            } else {
                                cell.setCellType(CellType.STRING);//br-Excel导入Cannot get a text value from a numeric cell
                                value = cell.getStringCellValue();
                            }
                            lCellStr.add(j, value);
                        } else {
                            lCellStr.add(j, "");
                        }
                        j++;

                    }
                    for (int k = 0; k < lCellStr.size(); k++) {
                        if ("".equals(lCellStr.get(k))) {
                            m++;
                        }
                    }
                    if (m < colNum) {
                        lRowObj.add(lCellStr);
                    }
                }
            }
            sheet = null;
            workbook.close();
            workbook = null;
        } catch (Exception e) {
            //e.printStackTrace();
        } finally {

        }
        return lRowObj;
    }
}
